package com.lyshop.guiyingshanding.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.lyshop.guiyingshanding.R;
import com.lyshop.guiyingshanding.bean.UserInfoBean;
import com.lyshop.guiyingshanding.bean.UserTicketBean;
import com.lyshop.guiyingshanding.utils.DataUtils;
import com.lyshop.guiyingshanding.utils.GlideUtils;
import com.lyshop.guiyingshanding.utils.Utils;
import com.lyshop.guiyingshanding.view.GoodCostDialog;
import com.zc.mychart.model.GoodsAllBean;
import com.zc.mychart.model.GoodsItemBean;
import com.zc.mychart.utils.ArithUtil;
import com.zc.mychart.utils.FormatUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MarketDefaultPagerAdapter extends PagerAdapter {
    private List<GoodsItemBean> mGoods_list;
    private GoodsAllBean mGoodsAllBean;
    private LayoutInflater inflater;
    private int mPosition;
    private ViewPager mViewPager; // 新增：保存ViewPager实例
    private GoodCostDialog mCostDialog;

    public MarketDefaultPagerAdapter(List<GoodsItemBean> goods_list, GoodsAllBean goodsAllBean,LayoutInflater inflater, ViewPager viewPager) {
        this.mGoods_list = goods_list;
        this.mGoodsAllBean = goodsAllBean;
        this.inflater = inflater;
        this.mViewPager = viewPager;
    }

    public void setGoodsList(List<GoodsItemBean> mGoods_list) {
        this.mGoods_list = mGoods_list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mGoods_list.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        GoodsItemBean goodsItemBean = mGoods_list.get(position);
        View itemView = inflater.inflate(R.layout.item_market_default, container, false);
        RelativeLayout rl_goods_bg = itemView.findViewById(R.id.rl_goods_bg);
        ImageView iv_goods_img = itemView.findViewById(R.id.iv_goods_img);
        TextView tv_goods_name = itemView.findViewById(R.id.tv_goods_name);
        TextView tv_weight = itemView.findViewById(R.id.tv_weight);
        TextView tv_unit_price = itemView.findViewById(R.id.tv_unit_price);
        TextView tv_goods_price = itemView.findViewById(R.id.tv_goods_price);
        TextView tv_goods_desc = itemView.findViewById(R.id.tv_goods_desc);
        ImageView iv_header = itemView.findViewById(R.id.iv_header);
        tv_goods_desc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null == mCostDialog){
                    mCostDialog = new GoodCostDialog(container.getContext());
                    mCostDialog.setGoodsMoney(mGoodsAllBean, mGoods_list.get(position));
                }
                if(!mCostDialog.isShowing()){
                    mCostDialog.show();
                }
            }
        });

        rl_goods_bg.setTag("rl_goods_bg" + position);
        tv_unit_price.setTag("tv_unit_price" + position);
        tv_goods_price.setTag("tv_goods_price" + position);

        if (mPosition == position){
            rl_goods_bg.setBackgroundResource(R.drawable.shape_round_4dp_solid_white_stroke_red_ea);
        }else {
            rl_goods_bg.setBackgroundResource(R.drawable.shape_round_4dp_solid_white_stroke_gray_c9);
        }
        GlideUtils.showImageOptions(itemView.getContext(), goodsItemBean.product_img, iv_goods_img);
        tv_goods_name.setText(goodsItemBean.pro_name);
        tv_weight.setText(FormatUtil.formatDouble4(goodsItemBean.weight));
        tv_unit_price.setText(FormatUtil.formatDouble2(mGoodsAllBean.latest_price));
        String money = FormatUtil.formatDouble2(ArithUtil.mul(mGoodsAllBean.latest_price, goodsItemBean.weight));
        tv_goods_price.setText(money);
        String goodsDesc = "交货期限：30天(<font color=\"#00AAFF\">其他杂费</font>，提货时收取)";
        Utils.setHtmlText(tv_goods_desc, goodsDesc);
        if(null != mGoodsAllBean && mGoodsAllBean.during_type == 0){
            iv_header.setVisibility(View.VISIBLE);
            iv_header.setImageResource(R.drawable.icon_home_xiu);
        }else{
            setTicketInfo(goodsItemBean,iv_header);
        }
        container.addView(itemView);
        return itemView;
    }

    private Map<String, Integer> ticketMap = new HashMap<>();

    public void setTicketInfo(GoodsItemBean proInfoItemBean,ImageView iv_header) {
        if(DataUtils.isLogin(iv_header.getContext())){
            ticketMap.clear();
            UserInfoBean bean = DataUtils.getUserInfo(iv_header.getContext());
            if (bean == null) return;
            List<UserTicketBean> list = bean.ticket;
            if (list != null) {
                for (UserTicketBean ticket : list) {
                    ticketMap.put(ticket.goods_id, ticket.count);
                }
            }
            Integer ticket = 0;
            ticket = ticketMap.get(proInfoItemBean.goods_id);
            if(null != ticket && ticket > 0){
                iv_header.setVisibility(View.VISIBLE);
                iv_header.setImageResource(R.drawable.icon_home_ticket);
            }else{
                iv_header.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    public void setPosition(int p) {
        this.mPosition = p;
        for (int i = 0; i < getCount(); i++) {
            RelativeLayout rl_goods_bg = (RelativeLayout) mViewPager.findViewWithTag("rl_goods_bg" + i);
            if (rl_goods_bg != null) {
                rl_goods_bg.setBackgroundResource(i == mPosition ? R.drawable.shape_round_4dp_solid_white_stroke_red_ea
                        : R.drawable.shape_round_4dp_solid_white_stroke_gray_c9);
            }
        }
    }

    public void setLatestPrice(String money, String goodMoney){
        for (int i = 0; i < getCount(); i++) {
            TextView tv_unit_price = (TextView) mViewPager.findViewWithTag("tv_unit_price" + i);
            TextView tv_goods_price = (TextView) mViewPager.findViewWithTag("tv_goods_price" + i);
            if (tv_unit_price != null && tv_goods_price != null) {
                tv_unit_price.setText(money);
                tv_goods_price.setText(goodMoney);
            }
        }
    }
}